//
//  HomeController.m
//  20240823
//
//  Created by 张伟 on 2024/8/24.
//

#import "HomeController.h"
#import "CallRecordCotroller.h"
#import "NumberCallController.h"
#import "ContactController.h"

@interface HomeController ()
@property (nonatomic) UISegmentedControl * navigationBarSegmennt;
@property (nonatomic) NSInteger segmentSelectIndex;
@property (nonatomic) CallRecordCotroller * recordController;
@property (nonatomic) NumberCallController * numberCallController;
@property (nonatomic) ContactController * contactController;

@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.title = localizedStringKey(@"home");
}

-(void)setupUI {
    _segmentSelectIndex = 0;
    self.navigationItem.titleView = self.navigationBarSegmennt;
    
    _recordController = CallRecordCotroller.new;
    _recordController.view.frame = self.view.bounds;	
    _numberCallController = NumberCallController.new;
    _numberCallController.view.frame = self.view.bounds;
    _contactController = ContactController.new;
    _contactController.view.frame = self.view.bounds;
    
    [self addChildViewController:_recordController];
    [self addChildViewController:_numberCallController];
    [self addChildViewController:_contactController];
    
    [self.view addSubview:_recordController.view];
    [self.view addSubview:_numberCallController.view];
    [self.view addSubview:_contactController.view];
    
    _numberCallController.view.hidden = true;
    _contactController.view.hidden = true;
}

-(void)segmentDidSeleectChange:(UISegmentedControl *)sender{
    
    NSInteger index = sender.selectedSegmentIndex;
    if (self.segmentSelectIndex == index) {
        return;
    }
    
    self.segmentSelectIndex = index;
    
    switch (self.segmentSelectIndex) {
        case 0:{
            self.recordController.view.hidden = false;
            self.numberCallController.view.hidden = true;
            self.contactController.view.hidden = true;
        }
            break;
        case 1:{
            self.recordController.view.hidden = true;
            self.numberCallController.view.hidden = false;
            self.contactController.view.hidden = true;
        }
            break;
        case 2:{
            self.recordController.view.hidden = true;
            self.numberCallController.view.hidden = true;
            self.contactController.view.hidden = false;
        }
            break;
        default:
            break;
    }
    
}

- (UISegmentedControl *)navigationBarSegmennt {
    if (!_navigationBarSegmennt) {
        NSArray * items = @[localizedStringKey(@"record"),localizedStringKey(@"call"),localizedStringKey(@"contacts")];
        _navigationBarSegmennt = [[UISegmentedControl alloc] initWithItems:items];
        _navigationBarSegmennt.backgroundColor = UIColor.whiteColor;
        _navigationBarSegmennt.selectedSegmentIndex = 0;
        [_navigationBarSegmennt addTarget:self action:@selector(segmentDidSeleectChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _navigationBarSegmennt;
}

@end
